package com.rul.mars.order.controller;

import com.rul.mars.mbg.domain.OrderDetail;
import com.rul.mars.mbg.domain.OrderParam;
import com.rul.mars.order.service.OrderService;
import com.rul.mars.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 订单服务接口
 *
 * @author RuL
 */
@Api(tags = "OrderController", description = "用户订单服务接口")
@RestController
@RequestMapping(path = "/basic/order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    /**
     * 根据订单Id获取订单详情
     *
     * @param id 订单Id
     * @return 订单详情
     */
    @ApiOperation("根据订单Id查询订单详情")
    @GetMapping("/detail/{id}")
    public CommonResult<OrderDetail> detail(@PathVariable(value = "id") Long id) {
        OrderDetail orderDetail = orderService.detail(id);
        return CommonResult.success(orderDetail);
    }

    /**
     * 用户取消订单
     *
     * @param id 订单Id
     * @return 操作结果
     */
    @ApiOperation("用户取消订单")
    @GetMapping("/cancel")
    public CommonResult cancelOrder(@RequestParam(value = "id") Long id) {
        if (orderService.cancelOrder(id)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 用户确认收货
     *
     * @param id 订单id
     * @return 操作结果
     */
    @ApiOperation("用户确认收货")
    @GetMapping("/confirmReceive")
    public CommonResult confirmReceive(@RequestParam(value = "id") Long id) {
        orderService.confirmReceive(id);
        return CommonResult.success();
    }

    /**
     * 用户删除订单
     *
     * @param id 订单id
     * @return 操作结果
     */
    @ApiOperation("用户删除订单")
    @GetMapping("/delete")
    public CommonResult deleteOrder(@RequestParam(value = "id") Long id) {
        if (orderService.deleteOrder(id)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 生成订单
     *
     * @param orderParam 订单参数
     * @return 订单生成结果
     */
    @ApiOperation("直接购买生成订单")
    @PostMapping("/generate")
    public CommonResult<OrderDetail> generateOrder(@RequestBody OrderParam orderParam) {
        OrderDetail orderDetail = orderService.generateOrder(orderParam);
        return CommonResult.success(orderDetail);
    }

    /**
     * 先租后买生成订单
     *
     * @param leaseId 租赁ID
     * @return 订单详情
     */
    @ApiOperation("先租后买生成订单")
    @PostMapping("/generateByLease")
    public CommonResult<OrderDetail> generateOrder(@RequestParam(value = "leaseId") Long leaseId) {
        OrderDetail orderDetail = orderService.generateOrder(leaseId);
        return CommonResult.success(orderDetail);
    }

}
